<template>
  <common-page title="船舶电表度数">
    <avue-crud
        ref="crud"
        v-model="form"
        class="crud-table"
        :option="tableOption"
        :data="list"
        :page.sync="page"
        :search.sync="searchForm"
        :table-loading="loading"
        :header-cell-class-name="() => 'crud-table-head'"
        :cell-class-name="() => 'crud-cell'"
        @search-change="searchChange"
        @refresh-change="getList()"
        @hook:mounted="$ele.tableMount($refs.crud)"
        @hook:beforeDestroy="$ele.tableDestroy($refs.crud)"
        @current-change="currentChange"
        @size-change="handleSizeChange"
        @row-del="rowDel"
    >
      <!--      <template #menuLeft>-->
      <!--        <el-button type="primary" @click="handleExport" :loading="isUpload">导出</el-button>-->
      <!--      </template>-->
      <template #tonRangeSearch>
        <div style="display: flex">
          <el-input-number
              v-model.trim="searchForm.startTotalTon"
              controls-position="right"
              :min="0"
              :max="searchForm.endTotalTon ? searchForm.endTotalTon : 999999999"
              clearable
          ></el-input-number>
          <span style="margin: 0 10px">至</span>
          <el-input-number
              v-model.trim="searchForm.endTotalTon"
              controls-position="right"
              :min="searchForm.startTotalTon ? searchForm.startTotalTon : 0"
              :max="999999999"
              clearable
          ></el-input-number>
        </div>
      </template>
      <template #searchMenu="{ size }">
        <el-button type="primary" :size="size" @click="searchChange">查 询</el-button>
        <el-button type="primary" plain :size="size" @click="resetForm">重 置</el-button>
      </template>
      <!--      <template #menu>-->
      <!--        <el-button type="text" @click="handle1()" icon="el-icon-location-information">定位</el-button>-->
      <!--      </template>-->
    </avue-crud>
    <!-- look -->
    <el-dialog title="定位" :visible.sync="view1" width="30%" custom-class="crud-dialog"
               :before-close="() => (view1 = false)">
      地图
      <span slot="footer" class="dialog-footer"> </span>
    </el-dialog>
  </common-page>
</template>

<script>
import { tableOption } from './tableOption'
import { getBridgePage } from '@/api/matters'
import fileDownload from 'js-file-download'

export default {
  data() {
    return {
      searchForm: {},
      isUpload: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      list: [],
      loading: true,
      form: {},
      shipTypeArr: [], // 船舶类型
      shipSTypeArr: [],
      homePortArr: [], // 船籍港
      registArr: [], // 登记机构
      view1: false
    }
  },
  computed: {
    tableOption() {
      return tableOption.call(this)
    },
    documentHeight() {
      return this.$store.state.app.documentClientHeight
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handle1() {
      this.view1 = true
    },
    resetForm() {
      this.$refs.crud.searchReset()
      this.page.currentPage = 1
      this.getList()
    },
    rowDel(form, index) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch(() => {
          })
    },

    getList() {
      this.loading = true
      getBridgePage({
        ...this.searchForm,
        pageNumber: this.page.currentPage,
        pageSize: this.page.pageSize
      })
          .then((res) => {
            res.result.records = [
              {
                p1: '飞达189',
                p2: '10',
                p3: '30',
                p4: '20',
                p5: ''
              },
              {
                p1: '金盛751',
                p2: '10',
                p3: '30',
                p4: '20',
                p5: ''
              },
              {
                p1: '国展156',
                p2: '10',
                p3: '30',
                p4: '20',
                p5: ''
              }
            ]
            this.list = res.result.records
            this.page.total = res.result.total
          })
          .catch((err) => {
          })
          .finally(() => {
            this.loading = false
            this.$refs.crud?.doLayout()
          })
    },
    currentChange(val) {
      this.page.currentPage = val
      this.getList()
      this.$refs.curd?.doLayout()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getList()
      this.$refs.curd.doLayout()
    },
    searchChange(form, done) {
      this.page.currentPage = 1
      this.getList()
      done && done()
    },
    // 船舶类型
    async getShipArr() {
      let {result} = await getDict('ship_type')
      this.shipTypeArr = result
      let {result: s} = await getDict('ship_large_type')
      this.shipSTypeArr = s
    },
    // 船籍港
    async getHomePortArr() {
      let {result} = await getDictByHome()
      this.homePortArr = result
    },
    // 登记机构
    async getRegistArr() {
      let {result} = await getDictByRegist()
      this.registArr = this.getPortData(result)
    },
    getPortData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children) {
          if (data[i].children.length < 1) {
            data[i].children = undefined
          } else {
            this.getPortData(data[i].children)
          }
        }
      }
      return data
    },
    // 导出
    async handleExport() {
      this.isUpload = true
      this.axios({
        url: '/api/supervise/tenant/export',
        methods: 'get',
        responseType: 'blob',
        params: this.searchForm,
        headers: {
          Authorization: `bearer ${window.$auth.getToken()}`
        }
      })
          .then((res) => {
            this.isUpload = false
            fileDownload(res.data, `监管船舶信息表.xls`)
          })
          .catch(() => {
            this.isUpload = false
          })
    }
  }
}
</script>
<style scoped lang="scss"></style>
